import { Inject, Get, Post, Body, Query } from '@midwayjs/decorator';
import { CoolController, BaseController, CoolUrlTag, TagTypes } from '@cool-midway/core';
import { Validate } from '@midwayjs/validate';
import { LoginService } from '../../service/login';
import { UserService } from '../../service/user';
import { RouterService } from '../../service/router/router';
import { AuthDTO, LoginDTO, RefreshTokenDTO } from '../../dto/user';


/**
 * 用户
 */
@CoolUrlTag({ key: TagTypes.IGNORE_TOKEN, value: ['login', 'refreshToken'] })
@CoolController()
export class UserController extends BaseController {
    @Inject()
    loginService: LoginService;

    @Inject()
    userService: UserService;

    @Inject()
    routerService: RouterService;

    /**
     * 登录
     * @param code 微信code
     * @param encryptedData 包括敏感数据在内的完整用户信息的加密数据
     * @param iv 加密算法的初始向量
     */
    @Post('/login')
    @Validate()
    async login(@Body() params: LoginDTO) {
        const { code } = params;
        const { success, message, data } = await this.loginService.login(code);
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 学生认证
     * @param id 学校ID
     * @param username 学号
     * @param password 密码
     */
    @Post('/auth')
    @Validate()
    async auth(@Body() params: AuthDTO) {
        const { id, username, password } = params;
        const { success, message, data } = await this.routerService.login(id, username, password);
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 刷新Token
     * @param refreshToken 刷新Token 
     */
    @Post('/refreshToken')
    @Validate()
    async refreshToken(@Body() params: RefreshTokenDTO) {
        const { refreshToken } = params;
        const { success, message, data } = await this.loginService.refreshToken(refreshToken);
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 用户信息
     * @param id 用户id
     */
    @Get('/getUserInfo')
    async getUserInfo(@Query('id') id?: number) {
        const { success, message, data } = await this.userService.getUserInfo(id);
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 更换头像
     * @param url 头像地址
     */
    @Post('/changeAvatar')
    async changeAvatar(@Body('url') url: string) {
        const { success, message, data } = await this.userService.changeAvatar(url);
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 修改昵称
     * @param nickname 昵称
     */
    @Post('/changeNickname')
    async changeNickname(@Body('nickname') nickname: string) {
        const { success, message, data } = await this.userService.changeNickname(nickname);
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 修改手机号
     * @param mobile 手机号
     */
    @Post('/changeMobile')
    async changeMobile(@Body('mobile') mobile: string) {
        const { success, message, data } = await this.userService.changeMobile(mobile);
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }
}
